package com.xlhj.boot.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: caijliu
 * @Date: 02/09/2021 15:11
 * @Description Direct交换机配置类
 * @Vsersion: 0.0.1
 */
@Configuration
public class RabbitDirectConfig {

    private static final String DIRECT_EXCHANGE_NAME = "direct-exchange";
    private static final String DIRECT_QUEUE_NAME = "direct-queue";
    private static final String ROUTING_KEY = "direct.route.key";

    /**
     * 声明Exchange
     * @return
     */
    @Bean(name = "directExchange")
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE_NAME);
    }

    /**
     * 声明队列
     * @return
     */
    @Bean(name = "directQueue")
    public Queue directQueue() {
        return QueueBuilder.durable(DIRECT_QUEUE_NAME).build();
    }

    /**
     * 声明确认队列绑定关系
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding queueBinding(@Qualifier("directQueue") Queue queue, @Qualifier("directExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
}
